package com.lungle.warmdiary.app.view.wallet.fragment;

import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseListFragment;
import com.lungle.warmdiary.app.base.BaseSubscriber;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.response.AlipayAccount;
import com.lungle.warmdiary.app.model.response.TradeRecordList;
import com.lungle.warmdiary.app.model.response.WithdrawRecoder;
import com.lungle.warmdiary.app.ui.dialog.DialogHelper;
import com.lungle.warmdiary.app.utils.UIHelper;
import com.lungle.warmdiary.app.view.wallet.adapter.WithdrawRecordAdapter;
import com.umeng.analytics.MobclickAgent;

import java.io.InputStream;
import java.io.Serializable;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class WithDrawRecordFragment extends BaseListFragment<WithdrawRecoder> {
    private static String WITHDRAW_RECORD_SCREEN = "withdraw record screen";
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_pull_refresh_listview_header_tail;
    }


    @Override
    public void initView(View view) {
        LinearLayout LlTail = (LinearLayout) view.findViewById(R.id.ll_tail);
        LlTail.setVisibility(View.VISIBLE);
        View tailView = inflateView(R.layout.view_tail_trade);
        Button btnTakecash = (Button) tailView.findViewById(R.id.btn_takecash);
        btnTakecash.setOnClickListener(this);
        btnTakecash.setOnClickListener(this);
        LlTail.addView(tailView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        super.initView(view);

    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(WITHDRAW_RECORD_SCREEN);
        MobclickAgent.onResume(getActivity());

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(WITHDRAW_RECORD_SCREEN);
        MobclickAgent.onPause(getActivity());

    }

    @Override
    protected ListBaseAdapter getListAdapter() {
        return new WithdrawRecordAdapter();
    }

    @Override
    protected TradeRecordList parseList(InputStream is) throws Exception {
        return TradeRecordList.parse(is);
    }

    @Override
    protected TradeRecordList readList(Serializable seri) {
        return (TradeRecordList) seri;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mAdapter.getItem(position) != null)
            UIHelper.showWithdrawDetail(getActivity(), mAdapter.getItem(position));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_takecash:
                hasBindAlipay();
                break;
        }

    }

    @Override
    protected void requestData(boolean refresh) {
        sendRequestData();

    }

    @Override
    protected void sendRequestData() {
        ApiFactory.getUserApi().getWithdrawList(mCurrentPage * getPageSize(), getPageSize(), 0).subscribeOn(Schedulers.io())
                .compose(this.<Result>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(mSubscriber);
    }

    private void hasBindAlipay() {
        showWaitDialog();
        ApiFactory.getUserApi().getAliPay().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<Result<AlipayAccount>>() {
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        hideWaitDialog();
                    }

                    @Override
                    public void onNext(Result<AlipayAccount> alipayAccountResult) {
                        super.onNext(alipayAccountResult);
                        if (!TextUtils.isEmpty(alipayAccountResult.getData().getAccount())) {
                            UIHelper.showWithdrawCenter(getActivity(), alipayAccountResult.getData());
                        } else {
                            DialogHelper.getConfirmDialog(getActivity(), "未绑定支付宝", "是否现在绑定支付宝?", "绑定", "暂时不", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    UIHelper.showBindAccount(getActivity());
                                }
                            }, (dialog, which) -> dialog.dismiss()).show();
                        }
                    }

                    @Override
                    public void onCompleted() {
                        hideWaitDialog();
                    }
                });
    }


}
